package com.seeyon.ctp.portal.section;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.seeyon.ctp.portal.section.BaseSectionImpl;
import com.seeyon.ctp.portal.section.templete.BaseSectionTemplete;
import com.seeyon.ctp.portal.section.templete.HtmlTemplete;

public class GroupNewsSection extends BaseSectionImpl {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}
	
	@Override
    public String getId() {
        //栏目ID，必须与spring配置文件中的ID相同;如果是原栏目改造，请尽量保持与原栏目ID一致
        return "groupNewsSection";
    }

    @Override
    public String getName(Map<String, String> preference) {
        //栏目显示的名字，必须实现国际化，在栏目属性的“columnsName”中存储
        String name = preference.get("columnsName");
        if(StringUtils.isBlank(name)){
            return "中环集团新闻";
        }else{
            return name;
        }
    }

    @Override
    public Integer getTotal(Map<String, String> preference) {
        //栏目需要展现总数据条数时重写
        return null;
    }

    @Override
    public String getIcon() {
        // 栏目图标，暂不需要实现
        return null;
    }

    @Override
    public BaseSectionTemplete projection(Map<String, String> preference) {
        
        HtmlTemplete ht = new HtmlTemplete();
        StringBuilder html = new StringBuilder();
        html.append(crawlArticleList());
        ht.setHeight("230");
        ht.setHtml(html.toString());
        ht.setModel(HtmlTemplete.ModelType.inner);
        ht.setShowBottomButton(true);
        ht.addBottomButton(BaseSectionTemplete.BOTTOM_BUTTON_LABEL_MORE, "javascript:window.open('http://www.zhonghuan.com/articles/list.do?cid=101');");
        return ht;
    }
	
	public String crawlArticleList() {
		Document doc;
        StringBuffer sb = new StringBuffer("");
        int count = 0;
        try {
            doc = Jsoup.connect("http://www.zhonghuan.com/articles/list.do?cid=101").timeout(5000).get();
            Elements ListDiv = doc.getElementsByAttributeValue("class","chinel");
            for (Element element :ListDiv) {
                Elements ListLi = element.getElementsByTag("li");
                for (Element elementLi : ListLi) {
                	if(count > 9) {
                		break;
                	}
                	Elements links = elementLi.getElementsByTag("a");
                	Elements times = elementLi.getElementsByTag("span");
                	String strtime = "";
                    for (Element time : times) {
                    	strtime = time.text().trim();
                    }
                    for (Element link : links) {
                        String linkHref = link.attr("href");
                        String linkText = link.text().trim();
                        sb.append("<div style=\"font-size: 14px;padding-right:10px\">");
                        sb.append((new StringBuilder("<a href=\"")).append(linkHref).append("\" title=\"").append(linkText).append("\" target=\"_blank\">").append(linkText).append("</a>").toString());
                        sb.append("<span style=\"color:#8e8e8e;display:inline-block;float:right\">"  + strtime + "</span></div>");
                    }
                    count++;
                }
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

	
}
